<?php
class indexMod extends commonMod
{
	public function index()
	{
		$condition['uid']			= $_SESSION['user_id'];
		$info  = $this->model->table('admin')->where($condition)->find();
		$group=$this->model->table('group')->where("id='".$info['role_id']."'")->find();
		$this->assign('info',		$info);
		$this->assign('group_name',	$group['name']);
		$this->display();
	}
	
	//登录页面
	public function login()
	{
		if(!$this->isPost())
		{
			$this->display();
			return;
		}
		//获取数据
		$username=in($_POST['username']);
		$password= md5($this->config['CODE_PREFIX'].$_POST['password']);

		//数据验证
		if(empty($username))
		{
			$this->error('请输入用户名');
		}
		if(empty($_POST['password']))
		{
			$this->error('请输入密码');
		}
		if(empty($_POST['yzm']))
		{
			$this->error('请输入验证码');
		}
		if($_POST['yzm']!=$_SESSION['verify'])
		{
			$this->error('验证码错误,请重新输入!');
		}

		//数据库操作
		if($this->_login($username,$password))
		{
			$this->redirect(__APP__);
		}
		else
		{
			$this->error('用户名、密码错误,请重新输入!');
		}
	}
	
	public function _login($username,$password)
	{
		 $user_info = $this->model->table('admin')->where('username="'.$username.'"')->find(); 
        if(empty($user_info)){
            $this->error('帐号不存在！');
        }
        if($user_info['status'] == 0){
            $this->error('帐号已禁用！');
        }
		
		$condition = array();
		$condition['username'] = $username;
		$condition['password'] = $password;
		$user_info = $this->model->table('admin')->where($condition)->find();
		//用户名密码正确且没有锁定
		if(($user_info['password']==$password)&&($user_info['status']=='1'))
		{	
		 	$data = array();
            $data['logintime'] = time();
            $data['loginnum'] = $user_info['loginnum'] + 1;
            $data['ip'] = get_client_ip();
            $condition['uid'] = $user_info['uid'];
            //更新登录信息
            $this->model->table('admin')->data($data)->where($condition)->update();
			
			//设置登录信息
			$_SESSION['user_id']		= $user_info['uid'];
			$_SESSION['user_group_id']	= $user_info['role_id'];
			$_SESSION['user_name']		= $user_info['username'];
			$_SESSION['nicename']		= $user_info['nicename'];
			Auth::set($user_info['role_id']);//设置用户组，用来权限验证
			$_SESSION['__ROOT__']=__ROOT__;		
			return true;
		}
		return false;
	}
	
	//生成验证码
	public function verify()
	{
		Image::buildImageVerify();
	}
	
	//用户退出
	public function login_out()
	{
		unset($_SESSION['user_id']);
		unset($_SESSION['user_group_id']);
		unset($_SESSION['user_name']);
		Auth::clear();						//清除权限验证
		unset($_SESSION['__ROOT__']);
		$this->success('您已成功退出系统');
	}
	
	//欢迎页面
	public function welcome()
	{
		$condition['uid']= $_SESSION['user_id'];
		$info  = $this->model->table('admin')->where($condition)->find();
		$group=$this->model->table('group')->where("id='".$info['role_id']."'")->find();
		$category_num=$this->model->table('category')->count(); 
        $content_num=$this->model->table('content')->count(); 
        $content_num_o=$this->model->table('content')->where('status=0')->count(); 
		$this->assign('info',		$info);
		$this->assign('group_name',	$group['name']);
        $this->assign('category_num', $category_num);
        $this->assign('content_num', $content_num);
        $this->assign('content_num_o', $content_num_o);
		$this->display();
	}
	
}
?>